class ContactsController < ApplicationController
  before_filter :login_required, :authorization_required, :find_customer
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :create ], :redirect_to => { :action => :index }
  verify :method => :put, :only => [ :update ], :redirect_to => { :action => :index }
  verify :method => :delete, :only => [ :destroy, :destroy_selected ], :redirect_to => { :action => :index }
  
  def index
    if(@customer.nil?)
      respond_to do |format|
        format.xml {render :xml => Contact.find(:all).to_xml}
      end
    else
      respond_to do |format|
        format.html {render :action => 'list.rhtml'}
        format.js {render :action => 'list.rjs'}
        format.xml {render :xml => @customer.contacts.to_xml}
      end
    end
    
  end
  
  def search
    if params[:query].nil?
      @contacts = Contact.find(:all, :conditions => "enabled = true", :order => 'last_name, first_name');
    else
      @contacts = Contact.find(:all, :conditions => ["enabled = true and first_name like ? or last_name like ?", "%#{params[:query]}%", "%#{params[:query]}%"], :order => 'last_name, first_name')
    end
    render :action => 'search'
  end
  
  def show
    if(!(params[:id].nil?))
      @contact = Contact.find(params[:id])
    elsif (!@conversation.nil?)
      @contact = @conversation.contact
    end
    
    respond_to do |format|
      format.html {render :action => 'show.html.erb'}
      format.xml {render :xml => @contact.to_xml}
    end
  end
  
  def new
    @contact = Contact.new
    respond_to do |format|
      format.html {render :action => 'new.rhtml'}
      format.js {render :action => 'new.rjs'}
    end
  end
  
  def create
    @contact = Contact.new(params[:contact])
    if @contact.save
      @customer.contacts << @contact
      redirect_to :controller => 'customers', :action => 'show', :id => @customer unless request.xhr?
    else
      respond_to do |format|
        format.html {render :action => 'new.rhtml'}
        format.js {render :action => 'new.rjs'}
      end
    end
    
  end
  
  def edit
    @contact = Contact.find(params[:id])
    respond_to do |format|
      format.html {render :action => 'edit.rhtml'}
      format.js {render :action => 'edit.rjs'}
    end
  end
  
  def update
    @contact = Contact.find(params[:id])
    if @contact.update_attributes(params[:contact])
      redirect_to :controller => 'customers', :action => 'show', :id => @customer unless request.xhr?
    else
      render :action => 'edit' unless request.xhr?
    end
  end
  
  def destroy
    contact = Contact.find(params[:id])
    contact.enabled = false
    contact.save
    index
  end
  
  def destroy_selected
    @customer = Customer.find(params[:customer_id])
    unless params[:selected].nil?
      params[:selected].each do |id|
        contact = Contact.find(id)
        contact.enabled = false
        contact.save
      end
    end
    redirect_to :controller => 'customers', :action => 'show', :id => @customer unless request.xhr?
  end

end